﻿#region
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using Y2M.Components;
using Y2M.Controls;
using Y2M.Properties;

#endregion

namespace Y2M.Forms
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (DesignMode || RunTimeChecker.IsRunTime == false)
                return;

            txtRoot.Text = Settings.Default.DownloadRoot;

            Text += " - " + Utility.GetPublishVersionText();
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            if (DesignMode || RunTimeChecker.IsRunTime == false)
                return;

            ResumeFailedDownloads();
        }

        private void ResumeFailedDownloads()
        {
            var list = new List<string>(DownloadingList.Instance.Get());

            if (list.Count == 0)
                return;

            var answer = MessageBox.Show(string.Format("{0:N0} 건의 실패한 다운로드가 있습니다. 다시 다운로드할까요?", list.Count), "질문", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);

            if (answer != DialogResult.Yes)
                return;

            foreach (var url in list)
            {
                var clipInfo = YoutubeHelper.GetClipInfoFromUrl(url);

                if (clipInfo == null)
                {
                    MessageBox.Show("유효하지 않은 유튜브 URL 입니다.");
                    continue;
                }

                Debug.WriteLine(clipInfo.Title);

                uscDownloadManager.Add(clipInfo, true, url);
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            Settings.Default.Save();

            base.OnClosing(e);
        }

        private void ClipListControl_CommandExecuted(object sender, ClipListControl.CommandExecutedEventArgs e)
        {
            switch (e.Command)
            {
                case ClipCommand.GotoUtubePage:
                    OpenWebBrowser(e.Clip);
                    break;
                case ClipCommand.DownloadVideo:
                    Download(e.Clip, false);
                    break;
                case ClipCommand.DownloadAudio:
                    Download(e.Clip, true);
                    break;
            }
        }

        private void Download(Clip clip, bool audioOnly)
        {
            var clipInfo = YoutubeHelper.GetClipInfoFromClip(clip);

            if (clipInfo == null)
                return;

            uscDownloadManager.Add(clipInfo, audioOnly, clip.WatchPage.OriginalString);
        }

        private void OpenWebBrowser(Clip clip)
        {
            Form form = new WebBrowserForm(clip);
            form.Show();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (fbdBrowser.ShowDialog() == DialogResult.OK)
            {
                txtRoot.Text = fbdBrowser.SelectedPath;
                Settings.Default.DownloadRoot = fbdBrowser.SelectedPath;
            }
        }

        private void mnuFile_Exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void mnuHelp_About_Click(object sender, EventArgs e)
        {
            using (Form form = new AboutForm())
            {
                form.ShowDialog();
            }
        }

        private void mnuAction_Download_Click(object sender, EventArgs e)
        {
            using (UrlDownloadForm form = new UrlDownloadForm())
            {
                var dialogResult = form.ShowDialog();

                if (dialogResult != DialogResult.OK)
                    return;

                Cursor = Cursors.WaitCursor;

                var clipInfo = YoutubeHelper.GetClipInfoFromUrl(form.Url);

                if (clipInfo == null)
                {
                    MessageBox.Show("유효하지 않은 유튜브 URL 입니다.");
                    return;
                }

                uscDownloadManager.Add(clipInfo, form.AudioOnly, form.Url);

                Cursor = Cursors.Arrow;
            }
        }
    }
}